import 'dart:convert';

import 'package:app/model/user_vo.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

// 提供五套可选主题色
const _themes = <MaterialColor>[
  Colors.blue,
  Colors.cyan,
  Colors.teal,
  Colors.green,
  Colors.red,
];

/*
 *系统主题设置，包括系统默认字体 背景色等
 */
class GlobalConfig {
  static bool isDebug = true; //是否是调试模式
  static bool dark = false;
  static Color fontColor = Colors.black54;
  static String API_VERSION = "1.0";
  static String VERSION_KEY = "version";
  static const String BASE_URL = "http://bill-app-test.mldong.com";
  static String TOKEN_KEY = "Authorization";
  static SharedPreferences _prefs;
  static const String USER_CACHE_KEY = "__user__";
  static const String CURRENT_THEME = "__THEME__";
  static UserVo profile = UserVo();
  static String theme;

  // 可选的主题列表
  static List<MaterialColor> get themes => _themes;

  // 是否为release版
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  //初始化全局信息，会在APP启动时执行
  static Future init() async {
    _prefs = await SharedPreferences.getInstance();
    var _profile = _prefs.getString(USER_CACHE_KEY);
    if (_profile != null) {
      try {
        profile = UserVo.fromJson(jsonDecode(_profile));
      } catch (e) {
        print(e);
      }
    }
    theme = _prefs.getString(CURRENT_THEME);
    if (theme == null) {
      theme = "yellow";
    }
    // 如果没有缓存策略，设置默认缓存策略

    //初始化网络请求相关配置
  }

  // 持久化Profile信息
  static saveProfile() =>
      _prefs.setString(USER_CACHE_KEY, jsonEncode(profile.toJson()));
}
